﻿/*
 *	Copyright (c) 2014 UTUO WMFL, All rights reserved.
 *	http://www.wmfl.cn
 *
 *	@verson 1.0
 *	@author rguanghui(<a href="mailto:rguanghui.stupid@gmail.com">rguanghui.stupid@gmail.com</a>)
 *	@description some text for waiting write
 */
(function($){
	$.fn.extend({
		//消息通知(固定在浏览器顶部的居中文本)， type类型有: success/info/warning/danger（默认info）， msg: 弹出消息（默认无）
		alertMsg: function(configs) {
			var defaults = {
					type: "info",
					msg: ""
			};
			configs = $.extend({}, defaults, configs);
			var oMsg = $("<div class='alert alert-"+configs.type+" alert-dismissable'>"+configs.msg+"<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;</button></div>").appendTo(document.body);
			oMsg.css({
				position: "fixed",
				zIndex: "9999",
				top: "50px",
				maxWidth: "500px",
				overflow: "hidden",
				left: "50%",
				WebkitTransform: "translateX(-50%)",
				display: "none",
				borderRadius: "0 0 4px 4px"
			});
			oMsg.slideDown(600).delay(2000).slideUp(600, function(){oMsg.remove()});
		}
	});
})(jQuery);


(function($){ //复制粘贴
	$.fn.extend({
	  copyPaste: function( configs ) {
	    var clip = new ZeroClipboard.Client();
	    ZeroClipboard.setMoviePath("/Public/Style/Common/js/ZeroClipboard.swf");
	    clip.setText( configs.text );
	    clip.glue( configs.id );
	    clip.addEventListener("onComplete", function(){
	      $().alertMsg({type: "success", msg: "复制成功，使用 ctrl+v 复制到别处吧！"});
	    });  
	  }
	});
})(jQuery);



//给所有表单进行表单验证，根据data-loading-txt,data-ajax-url, 进行ajax提交
$(function(){
	$(".form-horizontal").Validform({
		tiptype: 3,
		datatype: {
			"phone" : /^(([04]\d{2,3}[-]?\d{7,8})|(1[3584]\d{9}))$/
		},
		callback:function(form){
			var _this = $(form).find("button[data-loading-txt]"); 
			var loadingGifUrl = "http://simlib.sinaapp.com/newwmfl/images/wmfl-loading-black.gif",
				ajaxUrl = $(_this).attr("data-ajax-url"),
				callback = $(_this).attr("data-action-callback"),
				tip = $(_this).parent().find("span");
			
			
			if(tip){
				tip.remove();
			};
			
			var oSpan = $("<span><img src=" + loadingGifUrl + " />" + $(_this).attr("data-loading-txt") + "</span>").appendTo($(_this).parent());
			
			$(_this).attr("disabled", true);
			$.post(ajaxUrl, $(".form-horizontal").serialize(), function(data){
				
				
				if(data.error == 1){
					oSpan.html("<span class='tip tip-danger'>"+data.msg+"</span>")
				}else if(data.error == 0){
					oSpan.html("<span class='tip tip-success'>"+data.msg+"</span>");
					callback && actionCallback(data.extra);
				}
				$(_this).removeAttr("disabled");
				
			}, "json");
			return false;
		}
	});
});

/* class中含有.actionds执行操作：删除，恢复，彻底删除
 * data-action-id 确定操作的数据
 * data-action-txt 操作时提示的文字
 * data-action-url 操作的地址
 * data-action-delete 是否是彻底删除
 */
$(function(){
	$(".actionds").click(function(){
		var actionUrl = $(this).attr("data-action-url"),//执行的URL
			actionId = $(this).attr("data-action-id"),//执行的id
			actionTxt = $(this).attr("data-action-txt"),//执行动作时的提示文字
			actionDel = $(this).attr("data-action-delete"),//是否是彻底删除按钮
			actionStatus = $(this).attr("data-action-status"),//设置用户状态用
			loadingGifUrl = "http://simlib.sinaapp.com/newwmfl/images/wmfl-loading-black.gif",//loading gif 地址
			tip = $(this).parent().find("span"),
			_this = this;
		
		if(actionDel){
			if(!window.confirm("彻底删除后无法恢复，确定要删除么？")){
				return;
			};
		};
		
		if(tip){
			tip.remove();
		};
		
		$(this).attr("disabled", true);
		
		var oSpan = $("<span style='position:absolute' class='tip tip-info'><img src=" + loadingGifUrl + " /> " +actionTxt+ "</span>").appendTo($(this).parent());
		
		$.post(actionUrl, {id: actionId}, function(data){
			if(data.error == 0){
				oSpan.html(data.msg);
				setTimeout(function(){
					$(_this).parent().parent().fadeOut(500);
				}, 1500);
				
			}else{
				oSpan.removeClass("tip-info").addClass("tip-danger");
				oSpan.html(data.msg);
				$(_this).removeAttr("disabled");
			}
			
		}, "json");

	});
});

/* class中含有.action-status执行更改状态操作
 * data-action-id 确定操作的数据
 * data-action-txt 操作时提示的文字
 * data-action-url 操作的地址
 */

$(function(){
	$(".action-status").click(function(){
		var actionUrl = $(this).attr("data-action-url"),//执行的URL
			actionId = $(this).attr("data-action-id"),//执行的id
			actionTxt = $(this).attr("data-action-txt"),//执行动作时的提示文字
			actionStatus = $(this).attr("data-action-status"),//设置用户状态用
			actionStatusTxt = $(this).attr("data-action-status-txt").split(" "), //更改状态时替换的文字
			loadingGifUrl = "http://simlib.sinaapp.com/newwmfl/images/wmfl-loading-black.gif",//loading gif 地址
			tip = $(this).parent().find("span"),
			_this = this;
		
		if(tip){
			tip.remove();
		};
		
		$(this).attr("disabled", true);
		
		var oSpan = $("<span style='position:absolute' class='tip tip-info'><img src=" + loadingGifUrl + " /> " +actionTxt+ "</span>").appendTo($(this).parent());
		
		$.post(actionUrl, {id: actionId, status: actionStatus}, function(data){
			
			if(data.error == 0){
				oSpan.html(data.msg).delay(1000).fadeOut(400);
				
				if(parseInt(actionStatus)){
					 $(_this).html(actionStatusTxt[0]); 
					$(_this).parent().siblings(".status").html(actionStatusTxt[1]);
					$(_this).attr("data-action-status", "0");
				}else{
					$(_this).html(actionStatusTxt[1]);
					$(_this).parent().siblings(".status").html(actionStatusTxt[0]);
					$(_this).attr("data-action-status", "1");
				}
				
			}else{
				oSpan.removeClass("tip-info").addClass("tip-danger");
				oSpan.html(data.msg);
				$(_this).removeAttr("disabled");
			}
			$(_this).removeAttr("disabled");
			
		}, "json");
		
	});
});


/*
 * 自动创建表情列表
 */
$(function(){
	$("#icon-emtion").click(function(){
		var emtionList = "",
			ul = $(this).parent().find("ul");
		for(var i=0; i<105; i++)
		{
			emtionList += "<li class='emtion-item'><i class='emtion-i'data-gifurl='http://5.wmei.sinaapp.com/Public/Style/My/Default/images/emtions/" + i + ".gif' style='background-position: " + (-i*24) +"px 0'></i></li>";
		}
		
		if( ul.length ){
			if( ul.is(":hidden") ){
				ul.fadeIn(400);
			}else{
				ul.fadeOut(400);
			}
		}else{
			$("<ul class='emtions'></ul>").html(emtionList).appendTo( $(this).parent() );
		}
		
		$(".emtion-i").click(function(){
			//var img = $( "<img src=' " + $(this).attr('data-gifurl') + " '/>" ).appendTo( $("#txtContent") );
			$("#content").html( "<img src=' " + $(this).attr('data-gifurl') + " '/>" )
		});
	});

});